package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.guigu.health.pojo.OrderSetting;
import com.guigu.health.service.OrderService;
import com.guigu.health.service.OrderSettingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:OrderSettingController
 * @Description:预约设置控制层
 * @Author:longtao
 * @Date:2021/4/12
 * @Version:1.0
 */
@RequestMapping("/orderSetting")
@Controller
public class OrderSettingController extends BaseController{
    /**
     * 调用orderSettingService业务
     */
    @Reference
    private OrderSettingService orderSettingService;
    /**
     * 调用orderService业务
     */
    @Reference
    private OrderService orderService;

    /**
     * 设置预约人数
     * @param orderSetting
     * @return
     */
    @RequiresPermissions(value = {"ORDER_SETTING"})
    @ResponseBody
    @RequestMapping(value = "/addOrderSetting")
    public Object addOrderSetting(@RequestBody OrderSetting orderSetting) {
        Object object;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(orderSetting.getOrderDate());
        long l = orderService.getTheNumberOfReserved(s);
        orderSetting.setReservations((int) l);
        if (orderSetting.getNumber() < l) {
            object = "flag";
            return object;
        }
        object = orderSettingService.addOrderSetting(orderSetting);
        return object;
    }

    /**
     * 显示预约设置数据
     * @return
     */
    @RequiresPermissions(value = {"ORDER_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/queryAllOrderSetting")
    public Object queryAllOrderSetting(String date){
        /**
         * 参数格式为2019-03
         */
        /**
         * 获取预约设置数据成功
         */
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return "flag";
        }
    }
}
